<?php

namespace app\api\service;

/**
 *  公众号消息
 */
class message
{
    /**
     * 发送推送消息
     */
    public function send_msg($data)
    {
        $res = [
            "thing32" =>['value'=>$data['name']],//业主姓名
            "character_string6" => ['value'=>$data['order_sn']],//账单编号
            "thing2" =>['value'=>$data['cost_name']], //缴费类型
            "time4" => ['value'=>$data['end_time']],//结算周期
            "amount3" =>  ['value'=>$data['amount'].'元'],//总金额
        ];
        return $this->send($res,config('wxapp.cost'),$data['openid']);
    }
    /**
     * 给商管推送派单消息
     */
    public function send_release($data=[]){
        $res = [
            "thing9" =>['value'=>$data['name']],
            "time7" => ['value'=>date('Y-m-d H:i:s',time())],
            "phone_number13" =>['value'=>$data['mobile']],
            "thing10" => ['value'=>$data['repair_name']],
            "thing6" => ['value'=>$data['reason']],
        ];
        return $this->send($res,config('wxapp.new_order'),$data['openid']);
    }
    /**
     * 给维修工师傅推送消息
     */
    public function send_master($data){
        $res = [
            "character_string2" =>['value'=>$data['order_sn']],//工单编号
            "thing3" => ['value'=>$data['repair_name']],//工单类型
            "thing9" =>['value'=>$data['reason']],//报修位置
            "short_thing10" => ['value'=>$data['name']],//服务人员
            "thing14" => ['value'=>$data['time']],//上门时间
        ];
        return $this->send($res,config('wxapp.master'),$data['openid']);
    }
    /**
     *缴费成功提醒
     */
    public function pay_success($data){
        $res = [
            "thing10" =>['value'=>$data['merchant_name']], //商家名称
            "character_string7" =>['value'=>$data['code']],//户号
            "amount3" => ['value'=>$data['amount'].'元'],//缴费金额
            "time4" =>['value'=>date('Y-m-d H:i')],//时间
            "thing9" => ['value'=>$data['cost_name']], //账单类型
        ];
        return $this->send($res,config('wxapp.pay_success'),$data['openid']);
    }
    /**
     *欠费停电通知
     */
    public function arrearage($data){
//        $res = [
//            "thing2" =>['value'=>$data['company']],  //用户名称
//            "amount3" =>['value'=>$data['balance'].'元'], //欠费金额
//            "time4" =>['value'=>date('Y-m-d')] //截止日期
//        ];
        $res = [
            'character_string1'=>'12313',
            'thing2'=>'12313',
            'character_string6'=>'12313',
            'time4'=>date('Y-m-d H:i:s'),
            'amount5'=>'12313',
        ];
        return $this->send($res,config('wxapp.arrearage'),$data['openid']);
    }
    /**
     *充值成功提醒
     */
    public function charge_success($data){
        $res = [
            "number8" =>['value'=>$data['code']],  //充值户号
            "thing16" =>['value'=>$data['company']],  //商家名称
            "amount14" =>['value'=>$data['amount'].'元'] //充值金额
        ];
        return $this->send($res,config('wxapp.charge_success'),$data['openid']);
    }
    public function send($data=[],$template_id='',$openid=''){
        $access_token =access_token(config('wxapp.app_id'),config('wxapp.secret'));
        $msgArray = [
            "touser" => $openid,
            "mp_template_msg" => [
                "appid" => config('wxapp.appid'),  //
                "template_id" =>$template_id,
                "url" => "",
                "miniprogram" => [
                    "appid" =>'wx7822f100925ec1ac',
                    "pagepath" => ""
                ],
                "data" => $data
            ]
        ];
//        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$access_token}";
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
        $res=httpRequest($url,'POST',json_encode($msgArray));
        $resl=json_decode($res,true);
        return $resl;
    }
}